import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
// import { getToken } from '@/utils/auth'
const service = axios.create({
  url: process.env.VUE_APP_BASE_API,
  timeout: 5000
})
service.interceptors.request.use(
  config => {
    // 添加请求头
    const token = store.getters.token
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  error => {
    // console.log(error)
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  response => {
    const { success, token, msg } = response.data
    if (success) {
      return response.data
    } else {
      Message.error(msg)
      return Promise.reject(new Error(msg))
    }
  },
  error => {
    return Promise.reject(error)
  }
)
export default service
